<hr>
<p>title: 392.is-subsequence<br>date: 2021-11-23 10:00:51<br>mathjax: true<br>tags:</p>
<pre><code>- LeeCode
</code></pre><p>categories: </p>
<pre><code>- LeeCode
</code></pre><p>hidden: true</p>
<h2 id="cateHidden-false"><a href="#cateHidden-false" class="headerlink" title="cateHidden: false"></a>cateHidden: false</h2><h3 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h3><blockquote>
<p>Given two strings s and t, return true if s is a subsequence of t, or false otherwise.</p>
<p>A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., “ace” is a subsequence of “abcde” while “aec” is not).</p>
</blockquote>
<h3 id="测试用例"><a href="#测试用例" class="headerlink" title="测试用例"></a>测试用例</h3><pre><code class="lang-bash">Input: s = &quot;abc&quot;, t = &quot;ahbgdc&quot;
Output: true

Input: s = &quot;axc&quot;, t = &quot;ahbgdc&quot;
Output: false
</code></pre>
<h3 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h3><p>该题要比<a href="/2021/11/22/LeeCode-by-JS/383-ransom-note/">[383] Ransom Note 赎金信</a>多了严格顺序的要求。</p>
<p>所以通过最后的索引位置进行判断。</p>
<pre><code class="lang-js">var isSubsequence = function(s, t) {
        let _s = 0; // 索引
        for(let i = 0; i &lt; t.length; i++) {
            if(s[_s] === t[i]) {
                _s++;
            }
        }
        return _s &gt;= s.length;
};
</code></pre>
<h3 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h3><blockquote>
<p>Accepted</p>
<p>17/17 cases passed (89 ms)</p>
<p>Your runtime beats 23.25 % of javascript submissions</p>
<p>Your memory usage beats 68.04 % of javascript submissions (38.9 MB)</p>
</blockquote>
